﻿using Microsoft.AspNetCore.Mvc;
using ChromaDB.Client;
using Furion.DynamicApiController;

namespace edan.zhjk.MCPWebApi.Controllers
{
    /// <summary>
    /// 测试
    /// </summary>
    [Route("[controller]")]
    [ApiDescriptionSettings("测试")]
    public class TestController : IDynamicApiController
    {
        private readonly HttpClient _httpClient;
        public TestController(IHttpClientFactory httpClientFactory)
        {
            //_httpClient = httpClientFactory.CreateClient("ChromaClient");
        }

        /// <summary>
        /// 测试
        /// </summary>
        /// <returns></returns>
        [HttpGet("get-test")]
        public string GetTest()
        {
            return "成功";
        }

        /// <summary>
        /// 测试  http://localhost:8000/docs/
        /// </summary>
        /// <returns></returns>
        [HttpGet("test-chroma")]
        public async Task TestChroma()
        {
            try
            {
                // 连接数据库
                var configOptions = new ChromaConfigurationOptions(uri: "http://localhost:8000/api/v1/");
                using var httpClient = new HttpClient();
                var client = new ChromaClient(configOptions, httpClient);

                // 创建集合（存储向量）
                var collection = await client.GetOrCreateCollection("movies");

                //对该集合执行操作
                var collectionClient = new ChromaCollectionClient(collection, configOptions, httpClient);

                //向集合中添加数据
                List<string> movieIds = ["1", "2", "3", "4", "5"];
                List<ReadOnlyMemory<float>> descriptionEmbeddings = [

                    new[] { 0.10022575f, -0.23998135f },
                    new[] { 0.10327095f, 0.2563685f },
                    new[] { 0.095857024f, -0.201278f },
                    new[] { 0.106827796f, 0.21676421f },
                    new[] { 0.09568083f, -0.21177962f },
                ];
                List<Dictionary<string, object>> metadata =
                [
                    new Dictionary<string, object> { ["Title"] = "The Lion King" },
                    new Dictionary<string, object> { ["Title"] = "Inception" },
                    new Dictionary<string, object> { ["Title"] = "Toy Story" },
                    new Dictionary<string, object> { ["Title"] = "Pulp Fiction" },
                    new Dictionary<string, object> { ["Title"] = "Shrek" },
                ];
                await collectionClient.Add(movieIds, descriptionEmbeddings, metadata);

                //搜索数据（使用向量搜索）
                List<ReadOnlyMemory<float>> queryEmbedding = [new([0.12217915f, -0.034832448f])];
                var queryResult = await collectionClient.Query(
                    queryEmbeddings: queryEmbedding,
                    nResults: 2,
                    include: ChromaQueryInclude.Metadatas | ChromaQueryInclude.Distances);
                foreach (var result in queryResult)
                {
                    foreach (var item in result)
                    {
                        Console.WriteLine($"Title: {(string)item.Metadata["Title"] ?? string.Empty} {(item.Distance)}");
                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
